

clear
clear mata
clear matrix
set more off
set memory 3000m
set matsize 10000
set maxvar 10000


** loads data
************************
#delimit;
use  year quarter mkttkcarrier mktnonstopmiles mkt nonstop mkt_totpassengers totpassengers avg_distance avg_tktroundtrip numrouteserved 
totnumrouteserved fraction_routes origin dest pop_origin pcinc_origin hub_origin treat_origin airportyear_rank_origin totyr_treat_origin cumyr_treat_origin 
part_cover_origin full_cover_origin either_cover_origin pop_dest pcinc_dest hub_dest treat_dest airportyear_rank_dest totyr_treat_dest cumyr_treat_dest 
part_cover_dest full_cover_dest either_cover_dest pct_origin_enplane_top2 pct_dest_enplane_top2 using "complete.dta", clear ;
#delimit cr
compress 


* drops repetitive observations
sort origin mkttkcarrier year quarter 
drop if (origin==origin[_n-1] & mkttkcarrier==mkttkcarrier[_n-1] & year==year[_n-1] & quarter==quarter[_n-1])


** drops carriers that do not serve enough passengers in a quarter to get accurate average fare
************************
drop if mkt_totpassengers<100



** rescales population and income
************************
replace pop_origin=pop_origin/10000
replace pcinc_origin=pcinc_origin/10000


** keeps markets where neither endpoint is "not a hub, N" or "small hub, S" between 2000 and 2009
************************
gen flag=0
replace flag=1 if (hub_origin=="N" | hub_origin=="S")
bysort origin: egen max_flag=max(flag)
drop if max_flag==1
drop flag max_flag


** drops markets where one of the endpoints hub_origin and hub_dest is never labeled 
************************
gen flag_origin=0
replace flag_origin=1 if (hub_origin=="M" | hub_origin=="L")
bysort origin: egen max_flag_origin=max(flag_origin)
drop if max_flag_origin==0
drop flag_origin max_flag_origin 



** drops airports that were not simultaneously concentrated and large enough (could be useful for identification, just too small #)
************************  

* large enough one year, but not concentrated enough
drop if origin=="ALB"
* only becomes big enough for one year in 2003, very concentrated
drop if origin=="ELP"
* large enough one year, but not concentrated enough
drop if origin=="GEG" 
* only becomes big enough from 2003 to 2009, then drops out in 2010, very concentrated
drop if origin=="MHT" 
* large enough one year, but not concentrated enough
drop if origin=="TUL" 
* only became concentrated enough in 2007 and drops out in 2010, also missed size cutoff in 2002
drop if origin=="TUS"

 
** flags some airports that were covered at a later time (JAX) or never had to file (SDF)
************************

gen flag_late=0
* covered in 2005, 2008, 2010
replace flag_late=1 if (origin=="LAS" | dest=="LAS")
* covered in only a couple years
replace flag_late=1 if origin=="JAX"
* covered in 2000, but never actually filed a plan
replace flag_late=1 if origin=="SDF"

*drop if (origin=="LAS" | dest=="LAS")
*drop if (origin=="JAX" | dest=="JAX")
*drop if (origin=="SDF" | dest=="SDF")


** defines "closeness" to the cutoff
************************
bysort origin: egen origin_max_top2=max(pct_origin_enplane_top2)
gen origin_absdiff=abs(origin_max_top2-.5)



* replaces number firms with logs
gen lev_numrouteserved=numrouteserved
replace numrouteserved=log(numrouteserved)


** allows the change for covered airports to be different
************************
gen byte cov=0
replace cov=1 if either_cover_origin==1


* gets some descriptives
su lev_numrouteserved if cov==1
su lev_numrouteserved if cov~=1




** sets up lag structure
************************

* keeps only first quarter of data
keep if quarter==1
keep if (year==1993 | year==1999 | year==2002 | year==2008)


* sets panel and time-series variables
xtset, clear
tsset, clear

sort mkt 
egen mkt_id=group(origin mkttkcarrier)
sort year quarter
egen time_id=group(year quarter)
sort mkt 
tsset mkt_id time_id, delta(1)


** generates first differences of variables
************************

foreach var of varlist lev_numrouteserved numrouteserved pop_origin pcinc_origin {
       
		gen d_`var'=`var'-L1.`var'
		
}

keep if (year==1999 | year==2008)

* generates second differences
xtset, clear
tsset, clear
drop time_id
sort year quarter
egen time_id=group(year quarter)
sort mkt year
tsset mkt_id time_id, delta(1)

gen dd_sample=1
foreach var of varlist lev_numrouteserved numrouteserved pop_origin pcinc_origin {
       
		gen dd_`var'=d_`var'-L1.d_`var'
		replace dd_sample=0 if dd_`var'==.
		
}


* summarizes pre and post fares for descriptive table
bysort origin mkttkcarrier: egen temp=max(dd_sample)
gen temp_cov=(cov==1)
bysort origin mkttkcarrier: egen max_temp_cov = max(temp_cov)


bysort max_temp_cov: su d_lev_numrouteserved if (temp==1 & year==1999)
bysort max_temp_cov: su d_lev_numrouteserved if (temp==1 & year==2008)

drop temp temp_cov



keep if year==2008


* clears xtset and tsset
xtset, clear
tsset, clear
drop mkt_id time_id

		


		



***********************************************************************
***********************************************************************




** SECOND DIFFERENCE
***************************

* all markets
reg dd_numrouteserved cov dd_pop_origin dd_pcinc_origin ///
if (origin_absdiff<1), vce(cluster origin)
estimates store r1dd 

* .2 window
reg dd_numrouteserved cov dd_pop_origin dd_pcinc_origin ///
if (origin_absdiff<.2), vce(cluster origin)
estimates store r2dd

* .1 window
reg dd_numrouteserved cov dd_pop_origin dd_pcinc_origin ///
if (origin_absdiff<.1), vce(cluster origin)
estimates store r3dd



xml_tab r1 r2 r3 r1dd r2dd r3dd, sd below stats(r2 r2_a r2_b r2_w N) ///
sheet("dd_network") append save("network_new.xls") ///
keep(cov dd_pop_origin dd_pcinc_origin)



